﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using mdryden.Core.Security;
using mdryden.Core.Settings;
using LastoRevival.Service.Implementations.Models;
using LastoRevival.Service.Contracts.Models;

namespace LastoRevival.Service.Implementations.Settings
{
	class ServiceSettings
	{

		static ServiceSettings instance;
		static object singletonLock = new object();

		public string ConnectionString { get; private set; }
		public string InstallationPath { get; private set; }
		public SimpleHash.HashType HashType { get; private set; }
		public int LoginMinutes { get; private set; }
		public int RememberMeDays { get; private set; }
		public UserVerificationType UserVerificationType { get; private set; }
		public string SmtpServer { get; private set; }
		public string SmtpUser { get; private set; }
		public string SmtpPassword { get; private set; }
		public int SmtpPort { get; private set; }
		public bool SmtpSsl { get; private set; }
		public string FromEmail { get; private set; }
		public string VerificationLink { get; private set; }
		public int ActiveSessionMinutes { get; private set; }
		
		public static ServiceSettings GetInstance()
		{
			if (instance == null)
			{
				lock (singletonLock)
				{
					if (instance == null)
					{
						instance = CreateInstance();
					}
				}
			}

			return instance;
		}


		private static ServiceSettings CreateInstance()
		{
			var userVerificationType = (UserVerificationType)Enum.Parse(typeof(UserVerificationType), SettingsHelper.LoadRequiredAppSetting("UserVerificationType"));

			var smtpServer = userVerificationType == UserVerificationType.Email ? 
				SettingsHelper.LoadRequiredAppSetting("SmtpServer") : 
				SettingsHelper.LoadAppSetting("SmptServer");
			
			var smtpPort = userVerificationType == UserVerificationType.Email ? 
				SettingsHelper.LoadRequiredAppSetting("SmtpPort") : 
				SettingsHelper.LoadAppSettingWithDefault("SmtpPort", "0");
			
			var smtpUser = userVerificationType == UserVerificationType.Email ? 
				SettingsHelper.LoadRequiredAppSetting("SmtpUser") : 
				SettingsHelper.LoadAppSetting("SmtpUser");
			
			var smtpPassword = userVerificationType == UserVerificationType.Email ? 
				SettingsHelper.LoadRequiredAppSetting("SmtpPassword") : 
				SettingsHelper.LoadAppSetting("SmtpPassword");
			
			var smtpSsl= userVerificationType == UserVerificationType.Email ? 
				SettingsHelper.LoadRequiredAppSetting("SmtpSsl") : 
				SettingsHelper.LoadAppSettingWithDefault("SmtpSsl", "false");

			var fromEmail = userVerificationType == UserVerificationType.Email ?
				SettingsHelper.LoadRequiredAppSetting("FromEmail") :
				SettingsHelper.LoadAppSetting("FromEmail");

			var newInstance = new ServiceSettings
			{
				ConnectionString = GetConnectionString(),
				InstallationPath = SettingsHelper.LoadRequiredAppSetting("InstallationPath"),
				HashType = GetHashType(),
				LoginMinutes = int.Parse(SettingsHelper.LoadRequiredAppSetting("LoginMinutes")),
				RememberMeDays = int.Parse(SettingsHelper.LoadRequiredAppSetting("RememberMeDays")),
				UserVerificationType = userVerificationType,
				SmtpServer = smtpServer,
				SmtpPort = int.Parse(smtpPort),
				SmtpUser = smtpUser,
				SmtpPassword = smtpPassword,
				SmtpSsl = bool.Parse(smtpSsl),
				FromEmail = fromEmail,
				ActiveSessionMinutes = int.Parse(SettingsHelper.LoadRequiredAppSetting("ActiveSessionMinutes")),
			};

			return newInstance;
		}

		private static string GetConnectionString()
		{
			var key = SettingsHelper.LoadRequiredAppSetting("ActiveConnection");
			var connectionString = ConfigurationManager.ConnectionStrings[key].ConnectionString;

			return connectionString;
		}

		private static SimpleHash.HashType GetHashType()
		{
			var key = SettingsHelper.LoadRequiredAppSetting("HashType");
			SimpleHash.HashType type;

			if (Enum.TryParse<SimpleHash.HashType>(key, out type))
			{
				return type;
			}
			else
			{
				throw new ConfigurationErrorsException("Hash type is invalid");
			}
		}
	}
}
